import json
from lxml import etree
from framework.core.crawler_result import Result


class LazyWidgetParser(object):
    @staticmethod
    def parse(response, current_task):
        parent_result = current_task.parent_result
        if isinstance(response.content, bytes):
            content = str(response.content, encoding="utf-8")
        else:
            content = response.content

        cr_lazy_widgets = [json.loads(t) for t in content.split("&&&") if t.strip() != ""]
        # 处理by features
        summary_attribute_tree = etree.HTML(cr_lazy_widgets[2][2])
        summary_attribute_list = summary_attribute_tree.xpath("//div[@id='cr-summarization-attributes-list']/descendant::div/span/text()")
        summary_attribute_feature_list = summary_attribute_list[::2]  # 奇数
        summary_attribute_score_list = summary_attribute_list[1::2]  # 偶数

        summary_attribute_item_list = []
        for idx, item in enumerate(summary_attribute_feature_list):
            feature_item = dict()
            feature_item["feature_name"] = summary_attribute_feature_list[idx]
            feature_item["feature_score"] = summary_attribute_score_list[idx]
            summary_attribute_item_list.append(feature_item)


        # lst = list(zip(["feature"] * len(summary_attribute_feature_list), summary_attribute_feature_list))
        # featrue_item =
        # summary_attribute_feature_list = dict(zip())
        # summary_attribute_dict = dict(zip(summary_attribute_feature_list, summary_attribute_score_list))

        # 处理
        summarization_lighthut_tree = etree.HTML(cr_lazy_widgets[0][2])
        summarization_lighthut_list = [t.strip() for t in summarization_lighthut_tree.xpath("//div[@id='cr-lighthut-1-']/descendant::a/span/text()")]

        detail = dict()
        detail["summary_attribute"] = summary_attribute_item_list
        detail["summary_lighthut"] = summarization_lighthut_list

        detail_result = Result.from_task(task=current_task, details=detail)

        return detail_result, None


if __name__ == "__main__":
    import json
    from framework.mock import MockResponse, MockTask
    parser = LazyWidgetParser()
    sampler_local_html = "./samples/stream"
    response = MockResponse(sampler_local_html)
    parser.parse(response, MockTask(None))
